//
//  CityTableViewController.m
//  解析全国天气
//
//  Created by Justsoso on 16/3/27.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "CityTableViewController.h"

#import "AreaTableViewController.h"

@interface CityTableViewController ()<NSXMLParserDelegate>

@property (nonatomic,strong) NSMutableArray *list;


@end

@implementation CityTableViewController
- (NSMutableArray *)list
{
    if (!_list)
    {
        _list = [NSMutableArray array];
    }
    return _list;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://flash.weather.com.cn/wmaps/xml/%@.xml",self.pyName]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    NSXMLParser *parper = [[NSXMLParser alloc] initWithData:data];
    parper.delegate = self;
    
    self.navigationItem.title = self.name;
    
    [parper parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict
{
    if ([elementName isEqualToString:@"city"])
    {
        [self.list addObject:attributeDict];
    }
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    
    NSDictionary *dic = self.list[indexPath.row];
    cell.textLabel.text = dic[@"cityname"];
    NSString *str = [NSString stringWithFormat:@"%@  %@°C ~ %@°C  %@",dic[@"stateDetailed"],dic[@"tem1"],dic[@"tem2"],dic[@"windState"]];
    cell.detailTextLabel.text = str;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.list[indexPath.row];
    AreaTableViewController *area = [[AreaTableViewController alloc] init];
    area.name = dict[@"cityname"];
    area.pyName = dict[@"pyName"];
    
    [self.navigationController pushViewController:area animated:NO];
    
}


@end
